package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SPU操作类
 */
@RequestMapping("/admin/product")
@RestController
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;


    //http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    //get  @RequestParam(value = "category3Id",required = true) == @RequestParam("category3Id") //此参数必须提交

    /**
     * 分页查询SpuInfo
     * @param category3Id
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<Page<SpuInfo>> spuInfoPageList(@RequestParam("category3Id") Long category3Id,
                                  @PathVariable("page") Long page,
                                  @PathVariable("limit") Long limit) {


        Page<SpuInfo> pageSettings = new Page<SpuInfo>(page,limit);


        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);

        Page<SpuInfo> infoPage = spuInfoService.page(pageSettings, wrapper);

        return Result.ok(infoPage);
    }


    /**
     * 保存SPU
     * 1、spu_info
     * 2、spu_image ： spu把后来所有sku要用的图片全部上传好，录入sku的时候sku自己选
     * 3、spu_sale_attr、spu_sale_attr_value
     *  先操作spu_sale_attr，保存以下值
     *      "baseSaleAttrId":"1",
     *      "saleAttrName":"选择颜色",
     *  再操作spu_sale_attr_value
     *      spuSaleAttrValueList:[
     *          {
     *              "baseSaleAttrId":"1",
     *              "saleAttrValueName":"黑色"
     *              }
     *      ]
     *   注意：spu_sale_attr、spu_sale_attr_value有冗余存储项，
     *   需要提前设置好值
     *  http://api.gmall.com/admin/product/saveSpuInfo
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        boolean b = spuInfoService.bigSaveSpuInfo(spuInfo);

        return Result.ok();
    }




    /**
     * #查询某个spu的销售属性以及值得详情
     * SELECT si.id,si.spu_name,
     *        ssa.base_sale_attr_id,
     *        ssa.sale_attr_name,
     *        ssav.sale_attr_value_name
     * FROM spu_info si
     * LEFT JOIN spu_sale_attr  ssa
     *     ON si.id = ssa.spu_id
     * LEFT JOIN spu_sale_attr_value ssav
     *     ON ssa.spu_id = ssav.spu_id
     *     AND ssa.base_sale_attr_id = ssav.base_sale_attr_id
     * where si.id=24
     */


    /**
     * 查询SPU对应的图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>>  spuImageList(@PathVariable("spuId") Long spuId){

        QueryWrapper<SpuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);

        List<SpuImage> list = spuImageService.list(wrapper);
        return Result.ok(list);
    }


    /**
     * 根据spuId获取商品销售属性列表
     *    每一组销售属性的组合，应该对应一个SKU
     *  颜色：黄、蓝
     *  内存：128,256
     *  SKU： 2*2=4：
     *   黄+128  黄+256
     *   蓝+128  蓝+256
     *   我们在录入sku的时候，只需要录入我们的组合
     *
     * @param spuId
     * @return
     */
    //http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){

        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.geSpuAttrAndValue(spuId);

        return Result.ok(spuSaleAttrs);
    }



}
